package com.wn.controller;

import com.alibaba.fastjson.JSON;
import com.wn.bean.R;
import com.wn.service.StudentService;
import com.wn.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yellow Docter
 * @date 2022 -07-01
 * @desc
 */
@WebServlet("/delStu")
public class DelStuServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");

        // 01 拿到数据
        String id = request.getParameter("id");
        // 02 调用服务器
        StudentService studentService = new StudentServiceImpl();

        /**
         *  integer 是int的包装类
         *  Integer.parseInt(String s)  可以把字符串转成integer 类型
         *  Integer.value(String s)  可以把字符串转成integer 类型
         */
       boolean flag = studentService.delStu(Integer.parseInt(id));

        //03 回写数据
        String jsonString = JSON.toJSONString(flag ? R.ok() : R.error());
        response.getWriter().write(jsonString);

    }
}
